package com.example.demo.service.impl;

import com.example.demo.mapper.*;
import com.example.demo.model.*;
import com.example.demo.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class MyServiceImpl implements MyService {
    @Autowired
    BagMapper bagMapper;
    @Autowired
    ClothesMapper clothesMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    FurnitureMapper furnitureMapper;
    @Autowired
    DiaryMapper diaryMapper;
    @Autowired
    PlayerMapper playerMapper;
    @Autowired
    EnemyMapper enemyMapper;
    @Autowired
    ThingMapper thingMapper;

    @Override
    public List<Clothes> selectAllClothes() {
        ClothesExample example = new ClothesExample();
        List<Clothes> list = clothesMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<Product> selectAllProduct() {
        ProductExample example = new ProductExample();
        List<Product> list = productMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<Bag> selectAllBag() {
        BagExample example = new BagExample();
        List<Bag> list = bagMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<Furniture> selectAllFurniture() {
        FurnitureExample example = new FurnitureExample();
        List<Furniture> list = furnitureMapper.selectByExample(example);
        return  list;
    }

    @Override
    public List<Bag> selectByType(int type) {
        BagExample example = new BagExample();
        BagExample.Criteria criteria = example.createCriteria();
        criteria.andTypeEqualTo(type);
        List<Bag> list = bagMapper.selectByExample(example);
        return list;
    }

    @Override
    public List<Thing> selectAllThing() {
        ThingExample example = new ThingExample();
        List<Thing> list = thingMapper.selectByExample(example);
        return list;
    }

    @Override
    public int buyClothes(Clothes clothes) {
        Player player = playerMapper.selectByPrimaryKey(1);
        player.setMoney(player.getMoney()-clothes.getPrice());
        playerMapper.updateByPrimaryKey(player);

        int x = clothesMapper.updateByPrimaryKey(clothes);
        Bag bag = new Bag();
        bag.setImg(clothes.getImg());
        bag.setName(clothes.getName());
        bag.setIntroduce(clothes.getIntroduce());
        bag.setType(2);
        bagMapper.insert(bag);
        return x;
    }

    @Override
    public int buyProduct(Product product) {
        Player player = playerMapper.selectByPrimaryKey(1);
        player.setMoney(player.getMoney()-product.getPrice());
        playerMapper.updateByPrimaryKey(player);

        int x = productMapper.updateByPrimaryKey(product);
        Bag bag = new Bag();
        bag.setImg(product.getImg());
        bag.setName(product.getName());
        bag.setIntroduce(product.getIntroduce());
        bag.setType(3);
        bagMapper.insert(bag);
        return x;
    }

    @Override
    public List<Diary> readDiary() {
        DiaryExample example = new DiaryExample();
        List<Diary> list = diaryMapper.selectByExample(example);
        return list;
    }

    @Override
    public int writeDiary(String text) {
        Diary diary = new Diary();
        diary.setDate(new Date());
        diary.setContent(text);
        int x = diaryMapper.insert(diary);
        return x;
    }

    @Override
    public Player selectPlayer(int id) {
        Player player = playerMapper.selectByPrimaryKey(id);
        return player;
    }

    @Override
    public Enemy randomSelect(String area) {
        Enemy enemy = enemyMapper.randomSelect(area);
        return enemy;
    }

    @Override
    public int save(List<String> list) {
        int x ;
        for(String s:list){
            Thing t = thingMapper.selectByName(s);
            if(t==null){
                Thing thing = new Thing();
                thing.setName(s);
                thing.setAmount(1);
                x = thingMapper.insert(thing);
            }else{
                t.setAmount(t.getAmount()+1);
                x = thingMapper.updateByPrimaryKey(t);
            }

        }


        return 0;
    }

    @Override
    public int updateMoney(int money) {
        Player player = playerMapper.selectByPrimaryKey(1);
        player.setMoney(money+player.getMoney());
        int x = playerMapper.updateByPrimaryKey(player);
        return x;
    }

    @Override
    public void equip(Bag bag) {
        if(bag.getName().equalsIgnoreCase("弯刀")){
            Player player = playerMapper.selectByPrimaryKey(1);
            player.setAtk(50);
            playerMapper.updateByPrimaryKey(player);
        }
        if(bag.getName().equalsIgnoreCase("屠龙宝刀")){
            Player player = playerMapper.selectByPrimaryKey(1);
            player.setAtk(999);
            playerMapper.updateByPrimaryKey(player);
        }
        if(bag.getName().equalsIgnoreCase("玄武盔甲")){
            Player player = playerMapper.selectByPrimaryKey(1);
            player.setDef(999);
            playerMapper.updateByPrimaryKey(player);
        }
    }

    @Override
    public void useThing(Thing thing) {
        thing.setAmount(thing.getAmount()-1);
        thingMapper.updateByPrimaryKey(thing);
    }
}
